﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Repository
{
    public class BannerGroupManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public BannerGroupManagementModel GetModel(string pageType, string roleName, string id)
        {
            BannerGroupManagementModel model = new BannerGroupManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBannerGroup = GetAll();

            if (!string.IsNullOrEmpty(id))
            {
                model.BannerGroupInfo = GetSingle(id);
            }

            return model;
        }

        public BannerGroupManagementModel GetModel(string pageType, string roleName)
        {
            BannerGroupManagementModel model = new BannerGroupManagementModel();
            model.ListBannerGroup = GetAll();

            return model;
        }

        public List<OptionModel> GetAllItems()
        {
            List<OptionModel> list = new List<OptionModel>();

            var items = (from c in entities.BannerGroups
                         select c);

            list.Add(new OptionModel { ID = "-1", Name = "All" });
            // Add items
            foreach (var item in items)
            {
                OptionModel model = new OptionModel();
                model.ID = item.ID.ToString();
                model.Name = item.Name;
                list.Add(model);
            }

            return list;
        }

        public List<BannerGroupModel> GetAll()
        {
            var items = (from c in entities.BannerGroups
                         orderby c.DateCreated descending
                         select c).ToList();
            List<BannerGroupModel> list = new List<BannerGroupModel>();

            foreach (var item in items)
            {
                BannerGroupModel model = new BannerGroupModel();
                model.ID = item.ID;
                model.Name = item.Name;
                model.Active = item.Active;
                model.DeleteDialog = string.Format("DeleteDialog('{0}')", item.ID);

                list.Add(model);
            }

            return list;
        }

        public BannerGroup GetSingle(string id)
        {
            return (from _object in entities.BannerGroups where _object.ID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] BannerGroup _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToBannerGroups(_object);
            entities.SaveChanges();
        }

        public void Delete(string id)
        {
            var _object = (from _list in entities.BannerGroups where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] BannerGroup _object)
        {
            _object.EntityKey = (from BannerGroup ac in entities.BannerGroups where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}